package com.xxx.kjds.controller;


import com.xxx.kjds.entity.Company;
import com.xxx.kjds.entity.User;
import com.xxx.kjds.service.CompanyService;
import com.xxx.kjds.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private CompanyService companyService;

    //根据用户名查找用户类型
    @PostMapping(value = "/findUserTypeByName")
    @ResponseBody
    public String findUserTypeByName(HttpServletRequest request) {
        String uname = request.getParameter("uname");
        List<User> list = userService.selectAll();
        for (User user : list) {
            if (user.getName().equals(uname)) {
                return user.getAccountType();
            }
        }
        return "false";
    }


    //根据用户类型查找用户
    @RequestMapping("/selectUsers")
    @ResponseBody
    public String selectUsers(HttpServletRequest request) {
        String account_type = request.getParameter("account_type");
        List<User> userList = userService.selectByType(account_type);
        request.setAttribute("userList", userList);
        return "success";
    }

    @RequestMapping("/updateUserType")
    @ResponseBody
    public String updateUserType(HttpServletRequest request) {
        String id = request.getParameter("id");
        String account_type = request.getParameter("account_type");
        User user = userService.selectByPrimaryKey(Integer.parseInt(id));
        user.setAccountType(account_type);
        int result = userService.updateByPrimaryKey(user);
        if (result <= 0) {
            return "fail";
        }
        return "success";
    }

    @RequestMapping("/getUserAll")
    @ResponseBody
    public List<User> getUserAll() {
        return userService.selectAll();
    }

    @RequestMapping("/getUserAllNotAdmin")
    @ResponseBody
    public List<User> getUserAllNotAdmin(HttpServletRequest request) {
        List<User> result = new ArrayList<>();
        Integer admin_id = Integer.valueOf(request.getParameter("uid"));

        List<User> userList = userService.selectAll();
        for (User user : userList) {
            if (user.getId() != admin_id) {
                result.add(user);
            }
        }
        return result;
    }


    @RequestMapping("/getUserAllNotAdminByPage")
    @ResponseBody
    public List<User> getUserAllNotAdminByPage(HttpServletRequest request){
        int pageNo = Integer.parseInt(request.getParameter("pageNum"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));

        return userService.selectAll(pageNo, pageSize);
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public String deleteUser(HttpServletRequest request) {
        String id = request.getParameter("id");
        int result = userService.deleteByPrimaryKey(Integer.parseInt(id));
        if (result <= 0) {
            return "用户删除失败!";
        }
        return "删除用户成功!";
    }


    //根据uid查用户信息
    @PostMapping("/selectById")
    @ResponseBody
    public User selectById(HttpServletRequest request) {
        String uid = request.getParameter("uid");
        return userService.selectByPrimaryKey(Integer.valueOf(uid));
    }

    //修改用户信息
    @PostMapping("/updateUser")
    @ResponseBody
    public String updateUser(HttpServletRequest request) {

        Integer uid = Integer.valueOf(request.getParameter("id"));
        User user = userService.selectByPrimaryKey(uid);

        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        if (email.length() > 30) {
            return "false";
        }
        if (phone.length() > 11) {
            return "false";
        }
        user.setEmail(email);
        user.setPhone(phone);

        int result = userService.updateByPrimaryKey(user);
        if (result > 0) {
            return "true";
        }
        return "false";
    }


    @PostMapping("/selectCompanyByUid")
    @ResponseBody
    public Object selectCompanyByUid(Integer id) {
        User user = userService.selectByPrimaryKey(id);
        Integer cid = user.getCompanyId();
        if (cid == null) {
            return "false";
        }
        return companyService.selectByPrimaryKey(cid);
    }

    //修改用户密码
    @RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
    @ResponseBody
    public String updatePwd(HttpServletRequest request) {
        Integer uid = Integer.valueOf(request.getParameter("id"));
        User user = userService.selectByPrimaryKey(uid);

        String opw = request.getParameter("opw");
        String npw1 = request.getParameter("npw1");
        String npw2 = request.getParameter("npw2");

        //对输入判断
        if (!opw.equals(user.getPassword())) {
            return "输入的旧密码不正确，请重新输入！";
        } else if (!npw1.equals(npw2)) {
            return "输入的两次密码不一致,请重新输入!";
        }

        user.setPassword(npw1);
        int result = userService.updateByPrimaryKey(user);
        if (result > 0) {
            return "修改密码成功!";
        }
        return "修改密码成功!";
    }


}
